package com.cn.jdia.dc.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.jdia.dc.context.SpringContextUtils;
import com.cn.jdia.dc.model.Serverconfig;
import com.cn.jdia.dc.service.IServerconfigService;
import com.cn.jdia.dc.utils.SSHUtils;

@Controller
@RequestMapping("/svconfig")
public class SvConfigController {

	@Resource
	private IServerconfigService svconfigservice;
	
	@RequestMapping(value = "/test")
	@ResponseBody
	public String dbConnectTest(@RequestParam("ip") String ip,
			@RequestParam("svtype") String svtype, @RequestParam("username") String name,
			@RequestParam("password") String pwd, @RequestParam("contype") String contype) {
		
		int ret=-1;
		if(("SSH".equals(contype)) && ("Linux".equals(svtype))){
			ret=SSHUtils.sshConnect(ip, name, pwd);
		}
		
		return ret+"";
	}
	
	@RequestMapping(value = "/insert")
	@ResponseBody
	public String svconfigAdd(@RequestParam("sname") String sname,@RequestParam("ip") String ip,
			@RequestParam("svtype") String svtype, @RequestParam("username") String name,
			@RequestParam("password") String pwd, @RequestParam("contype") String contype){
		
		Serverconfig svconfig=new Serverconfig();
		svconfig.setServername(sname);
		svconfig.setIp(ip);
		svconfig.setServertype(svtype);
		svconfig.setUsername(name);
		svconfig.setPassword(pwd);
		svconfig.setConnecttype(contype);

		svconfigservice=(IServerconfigService)SpringContextUtils.getContext().getBean("svconfigService");
		int ret = svconfigservice.addServerConfig(svconfig);
		
		
		return ret+"";
	}
}
